﻿using System;
using System.Collections;
using System.IO;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using NLog;

namespace TfsGiggle.Core
{
    /// <summary>
    /// Stopwords for C#, C++ and JavaScript
    /// </summary>
    public class MultiLanguageAnalyzer : Analyzer
    {
        static Logger _logger = LogManager.GetCurrentClassLogger();
        static Lazy<Hashtable> _stopwordTable = new Lazy<Hashtable>(() => StopFilter.MakeStopSet(_stopWords));
        static string[] _stopWords = new string[] { 
            "abstract","alignas","alignof","and_eq","as","asm","auto","base","bitand","bitor","bool","break",
            "byte","case","catch","cdata","char","char16_t","char32_t","checked","class","code","compl","const","const_cast",
            "constexpr","continue","debugger","decimal","decltype","default","delegate","delete","do","double",
            "dynamic_cast","else","enum","event","explicit","export","extends","extern","false","finally","fixed",
            "float","for","foreach","friend","function","goto","if","implicit","import","in","inline","int",
            "interface","internal","is","list","lock","long","mutable","namespace","new","noexcept","not",
            "not_eq","null","nullptr","object","operator","or","or_eq","out","override","para","paramref",
            "params","private","protected","public","readonly","ref","register","reinterpret_cast","remarks",
            "return","sbyte","sealed","see","seealso","short","signed","sizeof","stackalloc","static",
            "static_assert","static_cast","string", "struct","summary","super","switch","template",
            "this","thread_local","throw","true","try","typedef","typeid","typename","typeof",
            "uint","ulong","unchecked","union","unsafe","unsigned","ushort","using",
            "value","var","virtual","void","volatile","wchar_t","while","with","xor","xor_eq","a", "an", "and", "are",
            "at","be", "but","by", "for", "if", "in", "into", "is", "it","no", "not", "of", "on", "or", "s", "such",
            "that", "the", "their", "then", "there","these","they", "this", "to", "was", "will", "with"};

        /// <summary>
        /// Gets tokens from source control documents
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public override TokenStream TokenStream(string fieldName, TextReader reader)
        {
            _logger.Trace("fieldName:{0}", fieldName);
            if (fieldName.Equals(SourceControlDocument.FieldNames.Contents))
            {
                return new StopFilter(true, new LowerCaseTokenizer(reader), _stopwordTable.Value, true);
            }
            return new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29).TokenStream(fieldName, reader);
        }
    }
}